/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Presentacion;

import AccesoADatos.GlobalException;
import AccesoADatos.NoDataException;
import AccesoADatos.ServicioUsuario;
import javax.swing.JOptionPane;

/**
 *
 * @author Fernando
 */
public class newUser extends javax.swing.JFrame {

    /**
     * Creates new form newUser
     */
    private Conectado cnx;
    private Principal pp;

    public newUser(Conectado cnx, Principal pp) {
        initComponents();
        this.setTitle("Creating new user");
        //this.setSize();
        this.cnx = cnx;
        this.pp = pp;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        l1 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        nU = new javax.swing.JTextField();
        Accept = new javax.swing.JButton();
        Cancel = new javax.swing.JButton();
        nP = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(450, 300));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        l1.setText("New User:");
        getContentPane().add(l1, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 60, -1, -1));

        jLabel1.setText("Password:");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 120, -1, -1));
        getContentPane().add(nU, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 60, 130, -1));

        Accept.setText("Accept");
        Accept.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AcceptActionPerformed(evt);
            }
        });
        getContentPane().add(Accept, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 180, -1, -1));

        Cancel.setText("Cancel");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });
        getContentPane().add(Cancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(210, 180, -1, -1));
        getContentPane().add(nP, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 120, 130, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void AcceptActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AcceptActionPerformed
        // TODO add your handling code here:
        //JOptionPane.showMessageDialog(null,this.cnx.getConexion().db);
        //String user,String pass,String ip,String port,String db
        ServicioUsuario su = new ServicioUsuario(this.cnx.getConexion().user, this.cnx.getConexion().pass, this.cnx.getConexion().ip, this.cnx.getConexion().port, this.cnx.getConexion().db);
        String newName = nU.getText();
        String newPass = nP.getText();
        boolean success = false;
        if (!(newName.equals("")) && !(newPass.equals(""))) {
            Object[] options = {"Yes", "No"};
            int op = JOptionPane.showOptionDialog(this, "Are you sure you want to create user " + newName + "?", "Creating User", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[1]);
            if (op == 0) {
                try {
                    //JOptionPane.showMessageDialog(null, newName+" , "+newPass);
                    success = su.newUser(newName, newPass);
                } catch (GlobalException ex) {
                } catch (NoDataException ex) {
                }
                if (!success) {
                    JOptionPane.showMessageDialog(this, "Can not create new User", "Warning", JOptionPane.ERROR_MESSAGE);
                } else {

                    Users us;
                    try {
                        us = new Users(this.cnx, this.pp);
                        this.pp.setContentPane(us);
                        this.pp.validate();
                        JOptionPane.showMessageDialog(null, "Success");
                    } catch (GlobalException ex) {
                        JOptionPane.showMessageDialog(null, "E1");
                        //Logger.getLogger(CreateUsers.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (NoDataException ex) {
                        JOptionPane.showMessageDialog(null, "E2");
                        //Logger.getLogger(CreateUsers.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            } else {
                JOptionPane.showMessageDialog(this, "User was not created.");
            }
        } else {
            JOptionPane.showMessageDialog(this, "There is at least one space empty.");
        }

    }//GEN-LAST:event_AcceptActionPerformed

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_CancelActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Accept;
    private javax.swing.JButton Cancel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel l1;
    private javax.swing.JPasswordField nP;
    private javax.swing.JTextField nU;
    // End of variables declaration//GEN-END:variables
}
